package com.android.shop.ui;

import com.android.shop.R;
import com.android.shop.api.MyHttpClient;
import com.android.shop.bean.SellerBean;
import com.android.shop.constant.MyConstantID;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SellerActivity extends BaseActivity {

	private EditText phone;
	private EditText minprice;
	private EditText arrivtime;
	private EditText sendneedprice;
	private EditText average_spend;
	SellerBean seller;
	private Button btn_saveseller;
	private String result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.seller_layout);
		phone=(EditText)this.findViewById(R.id.et_phone);
		minprice=(EditText)this.findViewById(R.id.et_minprice);
		arrivtime=(EditText)this.findViewById(R.id.et_arrivtime);
		sendneedprice=(EditText)this.findViewById(R.id.et_sendneedprice);
		average_spend=(EditText)this.findViewById(R.id.et_average_spend);
		btn_saveseller=(Button)this.findViewById(R.id.btn_saveseller);
		new Thread(new ReceiveThread()).start(); // 开启线程获取商家数据
		
		btn_saveseller.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				new Thread(new SubmitThread()).start();
			}
		});
	}
	
	class ReceiveThread implements Runnable {

		@Override
		public void run() {
			SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,Activity.MODE_PRIVATE);  
			String sellerid= sp.getString("sellerid", "");//获取商户id
		    seller=MyHttpClient.getSingle().getSeller(sellerid);// 从服务器获取类别
		    handler.sendEmptyMessage(11);
		}
	}
	
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(seller!=null){
				phone.setText(seller.getPhone());
				minprice.setText(seller.getMinprice());
				arrivtime.setText(seller.getArrivtime());
				sendneedprice.setText(seller.getSendneedprice());
				average_spend.setText(seller.getAverage_spend());
			}else{
				Toast.makeText(SellerActivity.this, "服务器繁忙，请稍后再试",
						Toast.LENGTH_LONG).show();
			}
		}
	};
	
	class SubmitThread implements Runnable {
		@Override
		public void run() {
			SubmitSeller();//提交
			handlerSubmit.sendEmptyMessage(11);
		}
	}

	Handler handlerSubmit = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (result.equals("SUC")) {
					Toast.makeText(getApplicationContext(), "保存成功",
							Toast.LENGTH_SHORT).show();
				}else if(result.equals("NOT FULL")){
					Toast.makeText(getApplicationContext(), "请将数据填写完整后保存", Toast.LENGTH_SHORT)
					.show();
				} else {
					Toast.makeText(getApplicationContext(), "网络异常",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
	
	private void SubmitSeller(){
		String _phone=phone.getText().toString();
		String _minprice=minprice.getText().toString();
		String _arrivtime=arrivtime.getText().toString();
		String _sendneedprice=sendneedprice.getText().toString();
		String _average_spend=average_spend.getText().toString();
		
		if(_phone.equals("")||_minprice.equals("")||_arrivtime.equals("")||_sendneedprice.equals("")||_average_spend.equals("")){
			result="NOT FULL";
			return;
		}
		
		SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,
				Activity.MODE_PRIVATE);
		String sellerid = sp.getString("sellerid", "");// 获取商户id
		result= MyHttpClient.getSingle().modifySeller(sellerid, _arrivtime, _minprice, _phone, _sendneedprice,_average_spend);
	}
	
	public void back(View v){
		this.finish();
	}
}
